//
//  HWGameCommentInteractMessageCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommentInteractMessageCell: UITableViewCell {
    
    var headImageView:UIImageView!
    var nameLabel:UILabel!
    var replyLabel:UILabel!
    /// 置顶标签
    var isTopTag:UILabel!
    var isVideoTag:UILabel!
    var titleLabel:UILabel!
    var contentLabel:UILabel!
    var timeLabel:UILabel!
    var replyButton:UIButton!
    
    var model:HWGameCommunityReplyMessageModel? {
        didSet {
            headImageView.kf.setImage(with: URL(string: model?.face ?? ""))
            nameLabel.text = model?.pet_name
            titleLabel.text = model?.title
            contentLabel.text = model?.content
            timeLabel.text = model?.time
            if model?.is_best == true {
                isTopTag.text = "置顶"
                isTopTag.backgroundColor = .colorWithHexString(hexString: "#FA8C08")
                isTopTag.isHidden = false
                isTopTag.snp.updateConstraints { make in
                    make.left.equalTo(18*widthScale)
                }
                if (model?.video.count ?? 0) > 0 {
                    ///为视频
                    isVideoTag.isHidden = false
                    isVideoTag.snp.makeConstraints { make in
                        make.left.equalTo(60*widthScale)
                    }

                    titleLabel.snp.updateConstraints { make in
                        make.left.equalTo(102*widthScale)
                    }
                } else {
                    isVideoTag.isHidden = true
                    titleLabel.snp.updateConstraints { make in
                        make.left.equalTo(60*widthScale)
                    }
                }
            } else {
                if model?.is_jiajing == true {
                    isTopTag.text = "精华"
                    isTopTag.backgroundColor = .colorWithHexString(hexString: "#FF726A")
                    isTopTag.isHidden = false
                    isTopTag.snp.updateConstraints { make in
                        make.left.equalTo(18*widthScale)
                    }
                    if (model?.video.count ?? 0) > 0 {
                        ///为视频
                        isVideoTag.isHidden = false
                        isVideoTag.snp.makeConstraints { make in
                            make.left.equalTo(60*widthScale)
                        }

                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(102*widthScale)
                        }
                    } else {
                        isVideoTag.isHidden = true
                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(60*widthScale)
                        }
                    }
                } else {
                    isTopTag.isHidden = true
                    if (model?.video.count ?? 0) > 0 {
                        isVideoTag.isHidden = false
                        isVideoTag.snp.makeConstraints { make in
                            make.left.equalTo(18*widthScale)
                        }

                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(60*widthScale)
                        }
                    } else {
                        isVideoTag.isHidden = true
                        titleLabel.snp.updateConstraints { make in
                            make.left.equalTo(18*widthScale)
                        }
                    }
                }

            }
        }
    }
    
    var replyButtonClick:((_:HWGameCommunityReplyMessageModel)->())?
    
    class func cellWithTableView(_ tableView:UITableView) -> (HWGameCommentInteractMessageCell) {
        let reUse:String = "HWGameCommentInteractMessageCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameCommentInteractMessageCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameCommentInteractMessageCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 14*widthScale
        headImageView.layer.masksToBounds = true
        headImageView.contentMode = .scaleAspectFill
        contentView.addSubview(headImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 13*widthScale)
        contentView.addSubview(nameLabel)
        
        replyLabel = HWLabel.createLabel(title: "回复", color: "#888888", font: 13*widthScale)
        contentView.addSubview(replyLabel)
    
        isTopTag = HWLabel.createBoldLabel(title: "置顶", color: "#FFFFFF", font: 11*widthScale)
        isTopTag.layer.cornerRadius = 4*widthScale
        isTopTag.layer.masksToBounds = true
        isTopTag.textAlignment = .center
        isTopTag.backgroundColor = .colorWithHexString(hexString: "#FA8C08")
        contentView.addSubview(isTopTag)
        
        isVideoTag = HWLabel.createBoldLabel(title: "视频", color: "#FFFFFF", font: 11*widthScale)
        isVideoTag.layer.cornerRadius = 4*widthScale
        isVideoTag.layer.masksToBounds = true
        isVideoTag.textAlignment = .center
        isVideoTag.backgroundColor = .colorWithHexString(hexString: "#333333")
        contentView.addSubview(isVideoTag)
        
        titleLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(titleLabel)
        
        contentLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        contentView.addSubview(contentLabel)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        contentView.addSubview(timeLabel)
        
        replyButton = UIButton(type: .custom)
        replyButton.setTitle("回复", for: .normal)
        replyButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        replyButton.titleLabel?.font = .systemFont(ofSize: 13*widthScale)
        replyButton.addTarget(self, action: #selector(replyButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(replyButton)
    }
    
    func initFrame() -> Void {
        headImageView.snp.makeConstraints { make in
            make.left.top.equalTo(18*widthScale)
            make.width.height.equalTo(28*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(5*widthScale)
            make.top.equalTo(headImageView.snp.top).offset(5*widthScale)
            make.height.equalTo(18*widthScale)
        }
        
        replyLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(5*widthScale)
            make.centerY.equalTo(nameLabel)
        }
        
        isTopTag.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(18*widthScale)
            make.height.equalTo(18*widthScale)
            make.width.equalTo(33*widthScale)
        }
        
        isVideoTag.snp.makeConstraints { make in
            make.left.equalTo(60*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(18*widthScale)
            make.height.equalTo(18*widthScale)
            make.width.equalTo(33*widthScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(54*widthScale)
            make.centerY.equalTo(isTopTag)
            make.right.equalTo(-18*widthScale)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(isTopTag.snp.bottom).offset(9*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(contentLabel.snp.bottom).offset(11*widthScale)
            make.bottom.equalTo(0)
        }
        
        replyButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.centerY.equalTo(timeLabel)
        }
    }
    
    @objc func replyButtonClick(_:UIButton) -> Void {
        self.replyButtonClick?(model ?? HWGameCommunityReplyMessageModel())
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
